package com.yanqu.road.server.cmd.activity.dunhuangshiku;

import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.pb.dunhuangshiku.DunHuangShiKuProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.goodsLimit.GoodsLimitModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.dunhuangshiku.DunHuangShiKuMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;

import java.math.BigInteger;

/**
 * @Description
 * @Author cwq
 * @Data 2021/4/19 18:05
 */
@Cmd(code = GameProtocol.S_DUN_HUANG_SHI_KU_RECOVER_ENERGY_FROM_CROSS, desc = "恢复体力返回消息到本服")
public class DunHuangShikuRecoverEnergyFromCrossCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        DunHuangShiKuProto.DunHuangShiKuRecoverEnergyRespMsg respMsg = DunHuangShiKuProto.DunHuangShiKuRecoverEnergyRespMsg.parseFrom(packet.getBytes());
        ret = respMsg.getRet();
        //恢复预扣除项
        Property property = new Property(respMsg.getGoodsId(), BigInteger.valueOf(respMsg.getNum()));
        player.getModule(PreReduceModule.class).restorePreReduce(property);
        if (ret == 0) {
            // 扣道具
            CurrencyModule module = player.getModule(CurrencyModule.class);
            module.removeCurrency(property, eLogMoneyType.DunHuangShiKu, eLogMoneyType.DunHuangShiKuRecoverEnergyCost);
        } else {
            // 失败了，如果是限制道具，返回一下数量
            if (DunHuangShiKuMgr.getShowLimitedUseStrengthItemIdList().contains(respMsg.getGoodsId())) {
                player.getModule(GoodsLimitModule.class).updateGoodsUseCount(respMsg.getGoodsId(), respMsg.getActivityId(), -respMsg.getNum());
            }
        }
        player.sendPacket(ClientProtocol.U_DUN_HUANG_SHI_KU_RECOVER_ENERGY, respMsg.toBuilder());
    }

    @Override
    public void sendData(GamePlayer player) {
    }
}
